<?php
namespace Admin\Controller;

class AdminController extends CommonController{
	public  $exec_comfunc  = 'index,add,edit,delete';//index,add,edit,delete
	//管理员列表
	public function index(){
		//按姓名搜索
		$realname = $this->_get('realname');
		if($realname){
			$where['realname'] = array('like','%'.$realname.'%');
		}
		$this->assign('realname',$realname);

		//按手机号搜索
		$mobile = $this->_get('mobile');
		if($mobile){
			$where['mobile'] = $mobile;
		}
		$this->assign('mobile',$mobile);

		//省份
		$province_id = $this->_get('province_id');
		$info['province_id'] = $province_id;
		if($province_id && !$city_id){
			$where['province_id'] = $province_id;
		}
		//城市
		$city_id = $this->_get('city_id');
		$info['city_id'] = $city_id;
		if($city_id){
			$where['city_id'] = $city_id;
		}
		$this->assign('info',$info);
		//客服城市
		if($this->_city_id){
			$where['city_id'] = $this->_city_id;
		}
		if(!$this->_is_supper_admin){
			$where['status'] = array('in', '0,1');
		}
		$list = parent::index('', $where, 'id', true);
		foreach ($list as $rs) {
			$where['id'] = $rs['role_id'];
			$rs['role_name'] = D('AdminRole')->where($where)->getField('title');
			$rs['city_name'] = $rs['city_id'] ? D('Area')->id2names($rs['city_id']) : '全部';
			$new_list[] = $rs;
		}
		$this->assign('list', $new_list);
		$this->display();
	}
	//个人信息
	public function info(){
		$where['id'] = $this->_user_id;
		$this->info = D('Admin')->where($where)->find();
		$this->display();
	}
	public function password_edit(){
		//提交处理
		if(IS_POST){
			$password    = $this->_post('password','对不起，密码不能为空');
			$newpassword = $this->_post('newpassword','对不起，确认密码不能为空');

			if($password!=$newpassword){
				$this->error('对不起，两次密码不一致！');
			}
			//重新设置密码
			if($password){
				$username = $this->_username;
				$data['password'] = password_md5($username,$password);
			}
		}
		$_GET['id'] = $this->_user_id;
		$this->_use_tpl = 'password_edit';
		parent::edit('',$data);
	}
	//添加管理员账号
	public function add(){
		$data = '';
		if(IS_POST){
			$username    = $this->_post('username','对不起，用户名不能为空');
			$password    = $this->_post('password','对不起，密码不能为空');
			$newpassword = $this->_post('newpassword','对不起，确认密码不能为空');
			$realname    = $this->_post('realname','对不起，姓名不能为空');
			$mobile      = $this->_post('mobile','对不起，手机号不能为空');

			$data['password'] = password_md5($username,$password);

			//权限
			$auths         = $_POST['auths'];
			$data['auths'] = serialize($auths);
		}
		//栏目信息
		$this->menu_info = C('MENU_LIST');
		$this->role_list = D('AdminRole')->select();

		$this->role_list = D('AdminRole')->select();
		parent::add('',$data);
	}
	//修改管理员账号
	public function edit(){
		$user_id = $this->_get('id');
		//提交数据
		$data = array();
		//提交处理
		if(IS_POST){
			$password    = $this->_post('password');
			$newpassword = $this->_post('newpassword');
			$realname    = $this->_post('realname','对不起，姓名不能为空');
			$mobile      = $this->_post('mobile','对不起，联系电话不能为空');
			$username    = $this->_post('username','用户名不能为空！');

			if($password!=$newpassword){
				$this->error('对不起，两次密码不一致！');
			}

			//重新设置密码
			if($password){
				$data['password'] = password_md5($username,$password);
			}else{
				//密码为空，不作修改
				$this->_filter_field = array('password');
			}
			//权限
			$auths         = $_POST['auths'];
			$data['auths'] = serialize($auths);
			// $this->_db_where['id'] = $user_id;
		}
		$where['id'] = $user_id;
		$auths_serialize = D('Admin')->where($where)->getField('auths');
		$auths = unserialize($auths_serialize);
		$this->_set_info['auths'] = $auths;
		
		//栏目信息
		$this->menu_info = C('MENU_LIST');
		$this->role_list = D('AdminRole')->select();

		$this->role_list = D('AdminRole')->select();
		parent::edit('',$data);
	}
	// 个人名片
	public function personal_card(){
		$where['id'] = $this->_user_id;
		$info = D('Admin')->where($where)->find();
		if(IS_POST){
			$this->_jump_url = U('personal_card');
			$_GET['id'] = $this->_user_id;
			// var_dump($_POST);exit;
			parent::edit('');
		}
		$this->assign('info', $info);
		$this->display();
	}
	public function win_select_user(){
		// 按用户ID搜索
		$user_id = $this->_get('user_id');
		if($user_id){
			$where['id'] = $user_id;
		}
		$this->assign('user_id', $user_id);

		// 按姓名搜索
		$realname = $this->_get('realname');
		if($realname){
			$where['realname'] = array('like', '%' . $realname . '%');
		}
		$this->assign('realname', $realname);

		// 按手机号搜索
		$mobile = $this->_get('mobile');
		if($mobile){
			$where['mobile'] = $mobile;
		}

		//省份
        $province_id = $this->_get('province_id');
        $info['province_id'] = $province_id;
        if($province_id && !$city_id){
            $where['province_id'] = $province_id;
        }
        //城市
        $city_id = $this->_get('city_id');
        $info['city_id'] = $city_id;
        if($city_id){
            $where['city_id'] = $city_id;
        }
        $this->assign('info', $info);
        //客服城市
        if($this->_city_id){
            $where['city_id'] = $this->_city_id;
        }
		$where['status'] = 1;
		$list = parent::index('', $where, 'create_time desc', true);
        unset($where);
        foreach ($list as $rs) {
        	$rs['user_id'] = $rs['id'];
            $data[] = $rs;
        }
        $this->assign('list', $data);
        $this->assign('is_mulit', $this->_get('is_mulit', '', '0'));
        $this->assign('user_field', $this->_get('user_field', '', 'user_id'));
        $this->display();
	}
}
?>